//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BlackCdsOptionEngine.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Termstructures/DefaultProbabilityTermStructure.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Quote.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::CBlackCdsOptionEngine (Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ probability, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ termStructure, Cephei::QL::IQuote^ vol) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CDefaultProbabilityTermStructure^ _Cprobability;
    CYieldTermStructure^ _CtermStructure;
    CQuote^ _Cvol;
    try
    {
#ifdef HANDLE
        _phBlackCdsOptionEngine = NULL;
#endif
        _Cprobability = safe_cast<CDefaultProbabilityTermStructure^> (probability);
        _Cprobability->Lock();
        Handle<QuantLib::DefaultProbabilityTermStructure>& _probability = static_cast<Handle<QuantLib::DefaultProbabilityTermStructure>&> (_Cprobability->GetHandle ()); 
        QuantLib::Real _recoveryRate = (QuantLib::Real)ValueHelper::Convert (recoveryRate);
        _CtermStructure = safe_cast<CYieldTermStructure^> (termStructure);
        _CtermStructure->Lock();
        Handle<QuantLib::YieldTermStructure>& _termStructure = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CtermStructure->GetHandle ()); 
        _Cvol = safe_cast<CQuote^> (vol);
        _Cvol->Lock();
        Handle<QuantLib::Quote>& _vol = static_cast<Handle<QuantLib::Quote>&> (_Cvol->GetHandle ()); 
        _ppBlackCdsOptionEngine = new boost::shared_ptr<QuantLib::BlackCdsOptionEngine> (new QuantLib::BlackCdsOptionEngine ( _probability,  _recoveryRate,  _termStructure,  _vol ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprobability != nullptr) _Cprobability->Unlock();
        if (_CtermStructure != nullptr) _CtermStructure->Unlock();
        if (_Cvol != nullptr) _Cvol->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::CBlackCdsOptionEngine (boost::shared_ptr<QuantLib::BlackCdsOptionEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBlackCdsOptionEngine = NULL;
#endif
	_ppBlackCdsOptionEngine = &childNative;
    
}
Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::CBlackCdsOptionEngine (QuantLib::BlackCdsOptionEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phBlackCdsOptionEngine = NULL;
#endif
	_ppBlackCdsOptionEngine = new boost::shared_ptr<QuantLib::BlackCdsOptionEngine> (&childNative);
    
    _BlackCdsOptionEngineOwner = owner;
    
}

Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::CBlackCdsOptionEngine (CBlackCdsOptionEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBlackCdsOptionEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackCdsOptionEngine = new boost::shared_ptr<QuantLib::BlackCdsOptionEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::CBlackCdsOptionEngine (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBlackCdsOptionEngine = NULL;
#endif
	if (!t->IsSubclassOf(CBlackCdsOptionEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::CBlackCdsOptionEngine (QuantLib::Handle<QuantLib::BlackCdsOptionEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBlackCdsOptionEngine = &childNative;
	_ppBlackCdsOptionEngine = &static_cast<boost::shared_ptr<QuantLib::BlackCdsOptionEngine>>(childNative.currentLink());
    
    _BlackCdsOptionEngineOwner = owner;
}
Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::CBlackCdsOptionEngine (QuantLib::Handle<QuantLib::BlackCdsOptionEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phBlackCdsOptionEngine = &childNative;
	_ppBlackCdsOptionEngine = &static_cast<boost::shared_ptr<QuantLib::BlackCdsOptionEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::CBlackCdsOptionEngine (QuantLib::BlackCdsOptionEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phBlackCdsOptionEngine = NULL;
#endif
	_ppBlackCdsOptionEngine = new boost::shared_ptr<QuantLib::BlackCdsOptionEngine> (new QuantLib::BlackCdsOptionEngine (childNative));
    
}
#endif

Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::~CBlackCdsOptionEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppBlackCdsOptionEngine != NULL)
    {
	    delete _ppBlackCdsOptionEngine;
        _ppBlackCdsOptionEngine = NULL;
    }
}
Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::!CBlackCdsOptionEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppBlackCdsOptionEngine != NULL)
    {
	    delete _ppBlackCdsOptionEngine;
    }
}
QuantLib::BlackCdsOptionEngine& Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::GetReference ()
{
    if (_ppBlackCdsOptionEngine == NULL) throw gcnew NativeNullException ();
	return **_ppBlackCdsOptionEngine;
}
boost::shared_ptr<QuantLib::BlackCdsOptionEngine>& Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::GetShared ()
{
    if (_ppBlackCdsOptionEngine == NULL) throw gcnew NativeNullException ();
	return *_ppBlackCdsOptionEngine;
}
QuantLib::BlackCdsOptionEngine* Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::GetPointer ()
{
    if (_ppBlackCdsOptionEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppBlackCdsOptionEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackCdsOptionEngine>& Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::GetHandle ()
{
	if (_phBlackCdsOptionEngine == NULL)
	{
		_phBlackCdsOptionEngine = new Handle<QuantLib::BlackCdsOptionEngine> (*_ppBlackCdsOptionEngine);
	}
	return *_phBlackCdsOptionEngine;
}
#endif
bool Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::HasNative () 
{
	return (_ppBlackCdsOptionEngine != NULL);
}

Cephei::QL::Experimental::Credit::IBlackCdsOptionEngine^ Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::Calculate::get ()
{
    try
    {
    	(*_ppBlackCdsOptionEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::IYieldTermStructure^ Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::TermStructure::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	Handle<QuantLib::YieldTermStructure> _rv = (Handle<QuantLib::YieldTermStructure>)(*_ppBlackCdsOptionEngine)->termStructure ( );   
        Cephei::QL::Termstructures::CYieldTermStructure^ _nrv = gcnew Cephei::QL::Termstructures::CYieldTermStructure (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IQuote^ Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine::Volatility::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	Handle<QuantLib::Quote> _rv = (Handle<QuantLib::Quote>)(*_ppBlackCdsOptionEngine)->volatility ( );   
        Cephei::QL::CQuote^ _nrv = gcnew Cephei::QL::CQuote (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IBlackCdsOptionEngine^ Cephei::QL::Experimental::Credit::CBlackCdsOptionEngine_Factory::Create (Cephei::QL::Termstructures::IDefaultProbabilityTermStructure^ probability, Double recoveryRate, Cephei::QL::Termstructures::IYieldTermStructure^ termStructure, Cephei::QL::IQuote^ vol)
{
    return gcnew CBlackCdsOptionEngine ( probability,  recoveryRate,  termStructure,  vol);
}
